package com.hdey.service.heng.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdey.mapper.heng.TbUscPermissionMapper;
import com.hdey.pojo.heng.TbUscPermission;
import com.hdey.pojo.heng.TbUscUser;
import com.hdey.service.heng.TbUscPermissionService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

@Service("tbUscPermissionService")
@Transactional
public class TbUscPermissionServiceImpl extends ServiceImpl<TbUscPermissionMapper, TbUscPermission> implements TbUscPermissionService {
   @Resource
    private TbUscPermissionMapper permissionMapper;

    @Override
    public List<String> findPermCodeList(Integer roleId) {
        return permissionMapper.findPermCodeList(roleId);
    }

    @Override
    public Map<TbUscPermission, List<TbUscPermission>> searchMenuList() {
        Map<TbUscPermission, List<TbUscPermission>> map=new TreeMap<>((p1,p2)->p1.getSort()-p2.getSort());
        Subject subject= SecurityUtils.getSubject();
        TbUscUser loginUser= (TbUscUser) subject.getPrincipal();
        List<TbUscPermission> list= permissionMapper.findByParentId(null);
        for (TbUscPermission p01:list){
            if (subject.isPermitted(p01.getPermCode())){
                List<TbUscPermission> list2=permissionMapper.findByParentId(p01.getId());
                List<TbUscPermission> list3= list2.stream().filter(permission -> subject.isPermitted(permission.getPermCode()))
                        .collect(Collectors.toList());
                map.put(p01,list3);
            }
        }
        return map;
    }
}
